﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dimebrain.TweetSharp.Extensions;
using Dimebrain.TweetSharp.Fluent;
using Dimebrain.TweetSharp.Model;

using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace TwitterTestApp
{
    public partial class Form1 : Form
    {

        int updateInterval = 60000; // Time to check for an updat, in miliseconds (1s = 1000ms)
        DirectMessage displayedMessage;
        String username = "PersonalTB";
        String password = "ic4nh4ztb";
        Timer statusTimer = new Timer();
        int scrollPos = 0;
        int scrollStep = 50;
        int scrollMax = 0;
        bool allCheck = false;
        bool tasksCheck = false;
        bool urgentCheck = false;
        bool unreadCheck = false;
        int unreadCounter = 0;

        BackgroundWorker bw_getDMs = new BackgroundWorker();
        BackgroundWorker bw_getFriends = new BackgroundWorker();


        MessageListContainer mlc = new MessageListContainer();
        readonly String messageListFilename = "MessageList.dat";

        IEnumerable<TwitterUser> friends = null;
        public Form1()
        {
            InitializeComponent();
            InitializeBackgroundWorkers();
            doMainGraphics();
            if (this.replyToComboBox.Items.Count == 0)
            {
                this.updateUserComboBox(this.username, this.password);
            }

            loadMessages();
            statusTimer.Tick += new EventHandler(myTimer_Tick);
            statusTimer.Interval = updateInterval;
            statusTimer.Start();
        }


        private void myTimer_Tick(object source, EventArgs e)
        {
            this.updateStatuses(this.username, this.password);
        }

        public void displayMessage(DirectMessage m)
        {
            this.deleteButton.Visible = true;
            this.displayedMessage = m;
            if (this.replyToComboBox.Items.Count == 0)
            {
                this.updateUserComboBox(this.username, this.password);
            }
            else
            {
                this.replyToComboBox.SelectedItem = this.displayedMessage.TDM.Sender.Name;
            }

            if (m != null)
            {
                //TwitterDirectMessage message = m.Dmessage;
                messageDisplayBox.Text = m.TDM.Text;
                largeUserPictureBox.ImageLocation = m.TDM.Sender.ProfileImageUrl;
                usernameDisplayBox.Text = m.TDM.Sender.Name;
                if (unreadCounter > 0) unreadCounter--;
                this.newMessageTextBox.Text = unreadCounter + " New Messages.";
            }

            this.displayMessageBoxes(getTypesFromCheckedBoxes());
            //else
            //{
            //    TwitterStatus message = m.Status;
            //    messageDisplayBox.Text = message.Text;
            //    largeUserPictureBox.ImageLocation = message.User.ProfileImageUrl;
            //    usernameDisplayBox.Text = message.User.Name;
            //}

        }

        public void scrollButtonLeftClick(object sender, EventArgs e)
        {
            int pos = this.scrollPos - this.scrollStep;
            if (pos > 0)
            {
                this.messageDisplayPanel.AutoScrollPosition = new Point(pos, 0);
                this.scrollPos = pos;
            }
            else
            {
                this.messageDisplayPanel.AutoScrollPosition = new Point(0, 0);
                this.scrollPos = 0;
            }

        }

        public void scrollButtonRightClick(object sender, EventArgs e)
        {
            int pos = this.scrollPos + this.scrollStep;
            if (pos < this.scrollMax)
            {
                this.messageDisplayPanel.AutoScrollPosition = new Point(pos, 0);
                this.scrollPos = pos;
            }
            else
            {
                this.messageDisplayPanel.AutoScrollPosition = new Point(this.scrollMax, 0);
                this.scrollPos = this.scrollMax;
            }

        }

        private void statusButton_Click(object sender, EventArgs e)
        {
            if (!bw_getDMs.IsBusy)
            {
                updateStatuses(this.username, this.password);
            }
        }

        private void tweetButton_Click(object sender, EventArgs e)
        {

            var message = this.textBox1.Text;
            if (message.Length != 0)
            {
                string status;
                if (this.replyToComboBox.SelectedItem.ToString() == "ALL")
                {
                    status = FluentTwitter.CreateRequest().Statuses().Update(message).AuthenticateAs(this.username, this.password)
                        .AsJson().Request();

                }
                else
                {
                    string username = null;
                    foreach (TwitterUser user in this.friends)
                    {
                        if (user.Name == this.replyToComboBox.SelectedItem.ToString())
                            username = user.ScreenName;
                    }
                    status = FluentTwitter.CreateRequest().DirectMessages()
                        .Send(username, message).AuthenticateAs(this.username, this.password)
                        .AsJson().Request();

                }
                var err = status.AsError();
                if (err == null)
                { // success
                    this.textBox1.Text = "Your message has been sent sucessfully.";
                }
                else
                {
                    MessageBox.Show(err.ErrorMessage);
                }
            }
        }

        private void updateUserComboBox(string username, string password)
        {
            if (!bw_getFriends.IsBusy)
                this.bw_getFriends.RunWorkerAsync((object)new object[] { (object)username, (object)password });

            //this.replyToComboBox.SelectedItem = this.displayedMessage.Dmessage.Sender.Name;
        }

        private void updateStatuses(string username, string password)
        {
            this.updateLabel.Text = DateTime.Now + ": Updating...";
            this.rateLimitLabel.Text = "Updating...";

            if (!bw_getDMs.IsBusy)
                bw_getDMs.RunWorkerAsync((object)new object[] { (object)username, (object)password });
        }

        private void createMessageBoxexTest()
        {
            for (int i = 0; i < 20; i++)
            {
                Message m = new Message("name" + i, "Message" + i);
                m.Form1 = this;
                //m.Height = panel1.Height;
                m.Location = new Point(10 + i * (m.Width + 10), 10);
                messageDisplayPanel.Controls.Add(m);
            }
        }

        private void updateMessageList(IEnumerable<TwitterDirectMessage> dmessages)
        {
            MessageListContainer newMessages = new MessageListContainer();
            foreach (var dmessage in dmessages)
            {
                var m = new DirectMessage(dmessage);
                if (!mlc.contains(m))
                {
                    m.IsNew = true;
                    newMessages.Add(m);
                }
            }
            newMessages.AddRange(mlc);
            mlc = newMessages;
        }

        private void displayMessageBoxes(DirectMessage.Types messageTypes)
        {
            IEnumerable<DirectMessage> messageList = this.mlc.getMessages(messageTypes);
            int x = 0;
            int count = 0;
            int space = 20;
            messageDisplayPanel.Controls.Clear();
            foreach (var m in messageList)
            {
                Message mb = m.createMessageBox();
                mb.Form1 = this;
                mb.Location = new Point(10 + x, (messageDisplayPanel.Height - 30 - mb.Height) / 2);
                messageDisplayPanel.Controls.Add(mb);
                x += mb.Width + space;
                count++;
            }
            this.scrollMax = 80 * count;

        }


        private void loadMessages()
        {
            FileStream fStream = null;
            BinaryFormatter bFormatter;
            try
            {
                fStream = new FileStream(messageListFilename, FileMode.Open, FileAccess.Read, FileShare.None);
                bFormatter = new BinaryFormatter();
                this.mlc = (MessageListContainer)bFormatter.Deserialize(fStream);
            }
            catch (FileNotFoundException ex)
            {
                ex.ToString();
                mlc = new MessageListContainer();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (fStream != null)
                    fStream.Close();
            }
            displayMessageBoxes(getTypesFromCheckedBoxes());
        }

        private void saveMessages()
        {
            FileStream fStream = null;
            BinaryFormatter bFormatter;
            try
            {
                fStream = new FileStream(messageListFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                bFormatter = new BinaryFormatter();
                bFormatter.Serialize(fStream, mlc);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (fStream != null)
                    fStream.Close();
            }
        }

        #region Graphics stuff for rounding
        private void doMainGraphics()
        {
            Pen myPenObject = new Pen(Color.Black);
            Graphics mdbGraphicsObject = this.messageDisplayBox.CreateGraphics();
            this.messageDisplayBox.Region = new Region(CurvedEdge(mdbGraphicsObject, myPenObject, 0, 0, messageDisplayBox.Width, messageDisplayBox.Height, 20));

            Graphics pb2GraphicsObject = this.largeUserPictureBox.CreateGraphics();
            this.largeUserPictureBox.Region = new Region(CurvedEdge(pb2GraphicsObject, myPenObject, 0, 0, largeUserPictureBox.Width, largeUserPictureBox.Height, 20));

            //Graphics cmb1GraphicsObject = this.replyToComboBox.CreateGraphics();
            //this.replyToComboBox.Region = new Region(CurvedEdge(cmb1GraphicsObject, myPenObject, 0, 0, replyToComboBox.Width, replyToComboBox.Height, 20));

            //Graphics tb1GraphicsObject = this.textBox1.CreateGraphics();
            //this.textBox1.Region = new Region(CurvedEdge(tb1GraphicsObject, myPenObject, 0, 0, textBox1.Width, textBox1.Height, 20));

            Graphics tb3GraphicsObject = this.newMessageTextBox.CreateGraphics();
            this.newMessageTextBox.Region = new Region(CurvedEdge(tb3GraphicsObject, myPenObject, 0, 0, newMessageTextBox.Width, newMessageTextBox.Height, 20));

            Graphics tb4GraphicsObject = this.usernameDisplayBox.CreateGraphics();
            this.usernameDisplayBox.Region = new Region(CurvedEdge(tb4GraphicsObject, myPenObject, 0, 0, usernameDisplayBox.Width, usernameDisplayBox.Height, 20));
            /*
            Graphics tb4GraphicsObject = this.textBox4.CreateGraphics();
            this.checkBox1.Region = new Region(CurvedEdge(tb4GraphicsObject, myPenObject, 0, 0, textBox4.Width, textBox4.Height, 20));
            Graphics tb4GraphicsObject = this.textBox4.CreateGraphics();
            this.textBox4.Region = new Region(CurvedEdge(tb4GraphicsObject, myPenObject, 0, 0, textBox4.Width, textBox4.Height, 20));
            Graphics tb4GraphicsObject = this.textBox4.CreateGraphics();
            this.textBox4.Region = new Region(CurvedEdge(tb4GraphicsObject, myPenObject, 0, 0, textBox4.Width, textBox4.Height, 20));
            Graphics tb4GraphicsObject = this.textBox4.CreateGraphics();
            this.textBox4.Region = new Region(CurvedEdge(tb4GraphicsObject, myPenObject, 0, 0, textBox4.Width, textBox4.Height, 20));
            */
        }

        public GraphicsPath CurvedEdge(Graphics gfxObj, Pen penObj, float X, float Y, float RectWidth, float RectHeight, float CornerRadius)
        {

            GraphicsPath gfxPath = new GraphicsPath();
            if (RectHeight < (CornerRadius * 2))
            {
                CornerRadius = RectHeight / 2;
            }

            gfxPath.AddLine(X + CornerRadius, Y, X + RectWidth - CornerRadius, Y);
            gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y, CornerRadius * 2, CornerRadius * 2, 270, 90);
            gfxPath.AddLine(X + RectWidth, Y + CornerRadius, X + RectWidth, Y + RectHeight - CornerRadius);
            gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
            gfxPath.AddLine(X + RectWidth - CornerRadius, Y + RectHeight, X + CornerRadius, Y + RectHeight);
            gfxPath.AddArc(X, Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
            gfxPath.AddLine(X, Y + RectHeight - CornerRadius, X, Y + CornerRadius);
            gfxPath.AddArc(X, Y, CornerRadius * 2, CornerRadius * 2, 180, 90);
            gfxPath.CloseFigure();

            return gfxPath;
        }

        public GraphicsPath FancyEdge(Graphics gfxObj, Pen penObj, float X, float Y, float RectWidth, float RectHeight, float CornerRadius)
        {

            GraphicsPath gfxPath = new GraphicsPath();
            if (RectHeight < (CornerRadius * 2))
            {
                CornerRadius = RectHeight / 2;
            }

            gfxPath.AddLine(X + CornerRadius, Y + (CornerRadius / 2), X + RectWidth - CornerRadius, Y + (CornerRadius / 2));
            gfxPath.AddLine(X + RectWidth - CornerRadius, Y + (CornerRadius / 2), X + RectWidth - CornerRadius, Y);
            gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y, CornerRadius * 2, CornerRadius * 2, 270, 90);
            gfxPath.AddLine(X + RectWidth, Y + CornerRadius, X + RectWidth - (CornerRadius / 2), Y + CornerRadius);
            gfxPath.AddLine(X + RectWidth - (CornerRadius / 2), Y + CornerRadius, X + RectWidth - (CornerRadius / 2), Y + RectHeight - CornerRadius);
            gfxPath.AddLine(X + RectWidth - (CornerRadius / 2), Y + RectHeight - CornerRadius, X + RectWidth, Y + RectHeight - CornerRadius);
            gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
            gfxPath.AddLine(X + RectWidth - CornerRadius, Y + RectHeight, X + RectWidth - CornerRadius, Y + RectHeight - (CornerRadius / 2));
            gfxPath.AddLine(X + RectWidth - CornerRadius, Y + RectHeight - (CornerRadius / 2), X + CornerRadius, Y + RectHeight - (CornerRadius / 2));
            gfxPath.AddLine(X + CornerRadius, Y + RectHeight - (CornerRadius / 2), X + CornerRadius, Y + RectHeight);
            gfxPath.AddArc(X, Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
            gfxPath.AddLine(X, Y + RectHeight - CornerRadius, X + (CornerRadius / 2), Y + RectHeight - CornerRadius);
            gfxPath.AddLine(X + (CornerRadius / 2), Y + RectHeight - CornerRadius, X + (CornerRadius / 2), Y + CornerRadius);
            gfxPath.AddLine(X + (CornerRadius / 2), Y + CornerRadius, X, Y + CornerRadius);
            gfxPath.AddArc(X, Y, CornerRadius * 2, CornerRadius * 2, 180, 90);
            gfxPath.AddLine(X + CornerRadius, Y, X + CornerRadius, Y + (CornerRadius / 2));
            gfxPath.CloseFigure();

            return gfxPath;
        }
        #endregion

        private void deleteButton_Click(object sender, EventArgs e)
        {
            var deleted = FluentTwitter.CreateRequest()
                .AuthenticateAs(this.username, this.password)
                .DirectMessages().Destroy(this.displayedMessage.TDM.Id).AsJson().Request();

            if (deleted.AsError() == null)
            { // success!
                this.deleteButton.Visible = false;
                this.largeUserPictureBox.ImageLocation = null;
                this.usernameDisplayBox.Text = "Welcome to Tweet Board";
                this.messageDisplayBox.Text = "Message successfully deleted.\n Please select another message from below.";
                this.mlc.Remove(this.displayedMessage);
                this.displayMessageBoxes(this.getTypesFromCheckedBoxes());
                //if (!bw_getDMs.IsBusy)
                //    this.updateStatuses(this.username, this.password);
            }
            else
            { // There was a problem
                MessageBox.Show("There was an error: " + deleted.AsError().ErrorMessage);
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            if (!bw_getDMs.IsBusy)
                this.updateStatuses(this.username, this.password);
        }

        #region BackgroundWorker stuff
        /// <summary>
        /// Initializes backgroundworker
        /// Adds DoWork and RunWorkerCompleted events
        /// </summary>
        private void InitializeBackgroundWorkers()
        {
            // Direct Message worker
            bw_getDMs.WorkerReportsProgress = false;
            bw_getDMs.WorkerSupportsCancellation = false;
            bw_getDMs.DoWork +=
                new DoWorkEventHandler(bw_getDMs_DoWork);
            bw_getDMs.RunWorkerCompleted +=
                new RunWorkerCompletedEventHandler(
                    bw_getDMs_RunWorkerCompleted);

            // Friend worker
            bw_getFriends.WorkerReportsProgress = false;
            bw_getFriends.WorkerSupportsCancellation = false;
            bw_getFriends.DoWork += new DoWorkEventHandler(
                bw_getFriends_DoWork);
            bw_getFriends.RunWorkerCompleted +=
                new RunWorkerCompletedEventHandler(
                    bw_getFriends_RunWorkerCompleted);
        }

        /// <summary>
        /// Sets up worker to fetch direct messages
        /// Username and password passed in as object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">object containing:
        /// object[0] = username
        /// object[1] = password</param>
        private void bw_getDMs_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            object[] obj = (object[])e.Argument;
            string username = obj.GetValue(0).ToString();
            string password = obj.GetValue(1).ToString();
            e.Result = this.getDMsWork(username, password, worker, e);
        }

        /// <summary>
        /// Fetches messages from twitter server
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="worker"></param>
        /// <param name="e"></param>
        /// <returns>List of direct messages of given user</returns>
        private IEnumerable<TwitterDirectMessage> getDMsWork(string username, string password, BackgroundWorker worker, DoWorkEventArgs e)
        {
            if (mlc.Count() > 1) // There are already messages stored
            {
                long id = mlc.getMostRecent().Id;

                return FluentTwitter.CreateRequest().AuthenticateAs(username, password)
                    .DirectMessages().Received().Since(id).AsJson().Request().AsDirectMessages();
            }
            else // No messages found, get all messages
            {
                return FluentTwitter.CreateRequest().AuthenticateAs(username, password)
                    .DirectMessages().Received().AsJson().Request().AsDirectMessages();
            }
        }

        /// <summary>
        /// Runs after DoWork has completed.
        /// Displays message boxes and updates rate limit.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bw_getDMs_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                IEnumerable<TwitterDirectMessage> messages = e.Result as IEnumerable<TwitterDirectMessage>;
                this.updateLabel.Text = "Last updated: " + DateTime.Now;
                this.updateMessageList(messages);

                unreadCounter = mlc.Count(DirectMessage.Types.Unread);
                if (messages.Count() > 0)
                    this.displayMessageBoxes(getTypesFromCheckedBoxes());
                var rate = FluentTwitter.CreateRequest().Accounts().GetRateLimitStatus().AsJson().Request().AsRateLimitStatus();
                this.rateLimitLabel.Text = rate.RemainingHits + "/" + rate.HourlyLimit + " Hits remaining";
                this.newMessageTextBox.Text = unreadCounter + " New Messages.";
            }
        }

        private void bw_getFriends_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            object[] obj = (object[])e.Argument;
            string username = obj.GetValue(0).ToString();
            string password = obj.GetValue(1).ToString();
            e.Result = this.getFriendsWork(username, password, worker, e);
        }


        private IEnumerable<TwitterUser> getFriendsWork(string username, string password, BackgroundWorker worker, DoWorkEventArgs e)
        {
            var friends = FluentTwitter.CreateRequest().Users().GetFriends().AuthenticateAs(username, password).AsJson().Request();
            return friends.AsUsers();
        }

        private void bw_getFriends_RunWorkerCompleted(object senter, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                //var friends = e.Result as IEnumerable<TwitterUser>;
                //var friend = friends.Select(1) as TwitterUser;
                //friend.
                //this.replyToComboBox.DataSource = e.Result as IEnumerable<TwitterUser>;
                this.friends = e.Result as IEnumerable<TwitterUser>;
                foreach (TwitterUser user in this.friends)
                {
                    this.replyToComboBox.Items.Add(user.Name);
                }
                this.replyToComboBox.Items.Insert(0, "ALL");
                if (this.displayedMessage == null)
                    this.replyToComboBox.SelectedItem = "ALL";
                else
                    this.replyToComboBox.SelectedItem = this.displayedMessage.TDM.Sender.Name;
            }
        }

        #endregion


        private DirectMessage.Types getTypesFromCheckedBoxes()
        {
            DirectMessage.Types flags = 0;
            if (allCheck)
                return DirectMessage.Types.All;
            if (tasksCheck)
                flags |= DirectMessage.Types.Task;
            if (urgentCheck)
                flags |= DirectMessage.Types.Urgent;
            if (unreadCheck)
                flags |= DirectMessage.Types.Unread;

            return flags;
        }

        private void allCheckBox_CheckedChanged(object sender, MouseEventArgs e)
        {
            allCheck = !allCheck;
            allChangeAppearance();

            tasksCheck = false;
            tasksChangeAppearance();

            urgentCheck = false;
            urgentChangeAppearance();

            unreadCheck = false;
            unreadChangeAppearance();

            this.displayMessageBoxes(DirectMessage.Types.All);
        }

        private void urgentCheckBox_CheckedChanged(object sender, MouseEventArgs e)
        {
            allCheck = false;
            allChangeAppearance();

            urgentCheck = !urgentCheck;
            urgentChangeAppearance();

            this.displayMessageBoxes(getTypesFromCheckedBoxes());
        }

        private void tasksCheckBox_CheckedChanged(object sender, MouseEventArgs e)
        {
            allCheck = false;
            allChangeAppearance();

            tasksCheck = !tasksCheck;
            tasksChangeAppearance();

            this.displayMessageBoxes(getTypesFromCheckedBoxes());
        }

        private void unreadCheckBox_CheckedChanged(object sender, MouseEventArgs e)
        {
            allCheck = false;
            allChangeAppearance();

            unreadCheck = !unreadCheck;
            unreadChangeAppearance();

            this.displayMessageBoxes(getTypesFromCheckedBoxes());
        }

        Color BorderColorDown = Color.White;
        Color BackColorDown = Color.BlanchedAlmond;
        Color BorderColorUp = Color.Blue;
        Color BackColorUp = Color.White;

        private void unreadChangeAppearance()
        {
            if (unreadCheck)
            {
                this.unreadCheckBox.FlatAppearance.BorderColor = BorderColorDown;
                this.unreadCheckBox.BackColor = BackColorDown;
            }
            else
            {
                this.unreadCheckBox.FlatAppearance.BorderColor = BorderColorUp;
                this.unreadCheckBox.BackColor = BackColorUp;
            }
        }

        private void urgentChangeAppearance()
        {
            if (urgentCheck)
            {
                this.urgentCheckBox.FlatAppearance.BorderColor = BorderColorDown;
                this.urgentCheckBox.BackColor = BackColorDown;
            }
            else
            {
                this.urgentCheckBox.FlatAppearance.BorderColor = BorderColorUp;
                this.urgentCheckBox.BackColor = BackColorUp;
            }
        }

        private void tasksChangeAppearance()
        {
            if (tasksCheck)
            {
                this.tasksCheckBox.FlatAppearance.BorderColor = BorderColorDown;
                this.tasksCheckBox.BackColor = BackColorDown;
            }
            else
            {
                this.tasksCheckBox.FlatAppearance.BorderColor = BorderColorUp;
                this.tasksCheckBox.BackColor = BackColorUp;
            }
        }

        private void allChangeAppearance()
        {
            if (allCheck)
            {
                this.allCheckBox.FlatAppearance.BorderColor = BorderColorDown;
                this.allCheckBox.BackColor = BackColorDown;
            }
            else
            {
                this.allCheckBox.FlatAppearance.BorderColor = BorderColorUp;
                this.allCheckBox.BackColor = BackColorUp;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            int count = tb.Text.Length;
            label1.Text = (140 - count) + " characters remaining.";
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = "";
        }

    }
}
